package com.csun.nusing.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

/**
 * 一些系统util类 ClassName: SystemUtil
 * @Description: TODO
 * @author fzq
 * @date 2017-10-18
 */
public class AppUtils {

	private AppUtils() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");

	}
	
	/**
	 * 安装apk
	 * 这种方法通过 Intent 机制，调出系统安装应用，重新安装应用的话，会保留原应用的数据。
	 * @Description: 安装apk
	 * @param @param context
	 * @param @param apkFilePath   
	 * @return void  
	 * @throws
	 * @author fzq
	 * @date 2017-10-18
	 */
	public static void installApp(Context context, String apkFilePath){
		//判断文件是否存在
		if (!FileUtils.isFileExists(apkFilePath)) return;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.parse("file://" + apkFilePath), "application/vnd.android.package-archive");  
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
		context.startActivity(intent);  
	}
	

	/**
	 * 获取应用程序名称
	 */
	public static String getAppName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			int labelRes = packageInfo.applicationInfo.labelRes;
			return context.getResources().getString(labelRes);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * [获取应用程序版本名称信息]
	 * 
	 * @param context
	 * @return 当前应用的版本名称
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packageInfo.versionName;

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
